package com.amado.course50java8;


import java.util.Arrays;
import java.util.IntSummaryStatistics;
import java.util.List;
import java.util.Random;
import java.util.stream.Collectors;


public class Java809Stream {

	public void list() {
		List<String> strings = Arrays.asList("abc", "", "bc", "efg", "abcd", "", "jkl");
		//filter 过滤
		List<String> filtered = strings.stream().filter(string -> !string.isEmpty()).collect(Collectors.toList());
		System.out.println(filtered);
	}
	public void foreach() {
		Random random = new Random();
		random.ints().limit(2).forEach(System.out::println);
	}

	public void map() {
		List<Integer> numbers = Arrays.asList(3, 2, 2, 3, 7, 3, 5);
		// 获取对应的平方数
		// distinct:去重
		// map:Returns a stream consisting of the results of applying the given function
		// to the elements of this stream.
		// 译: 返回一个流，该流包含将给定函数应用于此流的元素的结果。
		List<Integer> squaresList = numbers.stream().map(i -> i * i).distinct().collect(Collectors.toList());
		System.out.println(squaresList);
	}
	public void filter() {
		List<String> strings = Arrays.asList("abc", "", "bc", "efg", "abcd","", "jkl");
		// 获取空字符串的数量
		long count = strings.stream().filter(string -> string.isEmpty()).count();
		System.out.println(count);
	}
	public void sorted() {
		Random random = new Random();
		System.out.println("sort---------------begin");
		random.ints().limit(4).sorted().forEach(System.out::println);
		System.out.println("sort---------------end");
	}
	public void parallelStream() {
		List<String> strings = Arrays.asList("abc", "", "bc", "efg", "abcd","", "jkl");
		// 获取空字符串的数量
		long count = strings.parallelStream().filter(string -> string.isEmpty()).count();
		strings.stream().filter(string -> string.isEmpty()).count();
	}
	public void collect() {
		List<String>strings = Arrays.asList("abc", "", "bc", "efg", "abcd","", "jkl");
		List<String> filtered = strings.stream().filter(string -> !string.isEmpty()).collect(Collectors.toList());
		System.out.println("筛选列表: " + filtered);
		String mergedString = strings.stream().filter(string -> !string.isEmpty()).collect(Collectors.joining(", "));
		System.out.println("合并字符串: " + mergedString);
	}
	public void stats() {
		List<Integer> numbers = Arrays.asList(3, 2, 2, 3, 7, 3, 5);
		IntSummaryStatistics stats = numbers.stream().mapToInt((x) -> x).summaryStatistics();
		System.out.println("列表中最大的数 : " + stats.getMax());
		System.out.println("列表中最小的数 : " + stats.getMin());
		System.out.println("所有数之和 : " + stats.getSum());
		System.out.println("平均数 : " + stats.getAverage());
	}
	public static void mapToInt() {
		List<Integer> numbers = Arrays.asList(1, 2, 2);
		System.out.println(numbers.stream().mapToInt((x) -> x).summaryStatistics().getSum());
		System.out.println(numbers.stream().mapToInt((x) -> x).sum());

	}

	public static void main(String[] args) {
		mapToInt();
	}
}
